/*
 * This servlet handles the manage students part of the website
 */
package controllers.managecontrollers;

import classes.Breadcrumbs;
import classes.HibernateUtil;
import dao.StudentDao;
import dao.UserDao;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.Student;
import models.User;

/**
 *
 * @author Achie
 */
public class ManageStudent extends HttpServlet {

    private HibernateUtil hu = new HibernateUtil();

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    /*
     * this method prepares the add/edit page of students
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if (request.getParameter("idremove") != null) { //if it is a delete
            if (request.getSession().getAttribute("userrole").equals("Administrator") || request.getSession().getAttribute("userrole").equals("Teacher")) { //if current logged in user = admin or teacher
                deleteStudent(request, response); //delete the student
            } else { //if it is not an administrator/teacher, redirect the user
                response.sendRedirect("index.jsp"); 
            }
        } else {
            long student_id = Long.parseLong(request.getParameter("id")); //get student id from the parameter

            StudentDao sd = new StudentDao(hu.openSession());//get requested student
            Student student = sd.get(student_id);
            request.setAttribute("student", student); //set student object as attribute

            // Breadcrumbs
            Breadcrumbs[] breadcrumbs = {
                new Breadcrumbs("Index", "Home"),
                new Breadcrumbs("Student", "Manage student progress"),
                new Breadcrumbs("ManageStudent/edit?id", "Edit student")
            };
            request.setAttribute("breadcrumbs", breadcrumbs);

            hu.close(); //close connection
            
            //redirect to the add edit students page
            RequestDispatcher dispatcher = request.getRequestDispatcher("../jsp/add_edit_students.jsp");
            dispatcher.forward(request, response);
        }
    }

    /*
     * this method controls the modification of a student
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        saveStudentEdit(request, response); //execute save student edit method
        response.sendRedirect("Student"); //afterwards, redirect to the manage student page
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    /*
     * this method saves the student
     */
    public void saveStudentEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        long student_id = Long.parseLong(request.getParameter("studentid"));

        //get the parameters
        String firstname = request.getParameter("firstname");
        String insertion = request.getParameter("insertion");
        String lastname = request.getParameter("lastname");
        String email = request.getParameter("email");
        String interests = request.getParameter("interests");

        //----------------------------------------------------------------------
        //save details
        //----------------------------------------------------------------------
        UserDao ud = new UserDao(hu.openSession()); //get user object
        User studuser = ud.get(student_id);
        studuser.setEmail(email);//save email

        StudentDao sd = new StudentDao(hu.openSession()); //get student object
        Student stud = sd.get(student_id);
        stud.setFirstname(firstname);
        stud.setInsertion(insertion);
        stud.setLastname(lastname);

        //save objects
        ud.save(studuser);//save user
        sd.save(stud);//save student
    }

    /*
     * this method deletes a student
     */
    public void deleteStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {
        long student_id = Long.parseLong(request.getParameter("idremove"));
        //----------------------------------------------------------------------
        //save details
        //----------------------------------------------------------------------
        UserDao ud = new UserDao(hu.openSession()); //get user object
        User studuser = ud.get(student_id);

        StudentDao sd = new StudentDao(hu.openSession());//get student object
        Student stud = sd.get(student_id);
        //----------------------------------------------------------------------
        sd.delete(stud);//save student
        ud.delete(studuser);//save user

        response.sendRedirect("../Student"); //redirect user to the manage student page
    }

    public void redirect(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        RequestDispatcher dispatcher = request.getRequestDispatcher("jsp/manage_students.jsp");
        dispatcher.forward(request, response);
    }
}
